package com.example.administrator.activity.Homework.Homework6.homework_2;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.administrator.activity.R;
import com.example.administrator.activity.parcitise.Storage.SQList.Myheleper;

public class Homework6_SqlistActivity extends AppCompatActivity {
   Button btn_insert,btn_select,btn_deleter,btn_update;
   Myheleper1 myheleper1;
   SQLiteDatabase db;
   EditText user,pass;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_homework6__sqlist);
        init();
        myheleper1 =new Myheleper1(this,"my2.db",null,4);
        db=myheleper1.getWritableDatabase();
        View.OnClickListener clickListener=new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String sql="";
                switch (v.getId()){
                    case R.id.insert:
                        sql="INSERT INTO study(name,pass)\n" +
                                "VALUES(?,?)";
                        db.execSQL(sql,new Object[]{user,pass});
                        Toast.makeText(Homework6_SqlistActivity.this, "添加成功", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.update:
                        sql="update study\n" +
                                "set pass=?\n" +
                                "where name=?";
                        db.execSQL(sql,new Object[]{pass,user});
                        Toast.makeText(Homework6_SqlistActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.delete:
                        sql="delete from study\n" +
                                "where name=?";
                        db.execSQL(sql,new Object[]{user});
                        break;
                    case R.id.select:
                        sql= "SELECT *\n" +
                                "FROM study";
                        Cursor cursor= db.rawQuery(sql,null);
                        while (cursor.moveToNext()){
                            String name=cursor.getString(cursor.getColumnIndex("name"));
                            String pass=cursor.getString(cursor.getColumnIndex("pass"));
                            Toast.makeText(Homework6_SqlistActivity.this, name+":"+pass, Toast.LENGTH_SHORT).show();
                            break;
                        }


                }
            }
        };
        btn_insert.setOnClickListener(clickListener);
        btn_select.setOnClickListener(clickListener);
        btn_select.setOnClickListener(clickListener);
        btn_update.setOnClickListener(clickListener);
        user.setOnClickListener(clickListener);
        pass.setOnClickListener(clickListener);
    }

    private void init() {
        btn_insert=findViewById(R.id.insert);
        btn_select=findViewById(R.id.select);
        btn_deleter=findViewById(R.id.delete);
        btn_update=findViewById(R.id.update);
        user=findViewById(R.id.edituser);
        pass=findViewById(R.id.editpass);

    }
}
